package cc.admin.modules.graduate.controller;

import cc.admin.common.api.vo.Result;
import cc.admin.common.aspect.annotation.AutoLog;
import cc.admin.common.sys.base.controller.BaseController;
import cc.admin.common.sys.query.QueryGenerator;
import cc.admin.common.sys.vo.LoginUser;
import cc.admin.modules.graduate.entity.Category;
import cc.admin.modules.graduate.entity.MultiChoiceQuestions;
import cc.admin.modules.graduate.entity.option;
import cc.admin.modules.graduate.service.IMultiChoiceQuestionsService;
import cc.admin.modules.sys.entity.SysCascade;
import cc.admin.modules.sys.service.ISysCascadeService;
import cc.admin.modules.graduate.entity.ChoiceErrorQuestionsMulti;
import cc.admin.modules.graduate.service.IChoiceErrorQuestionsMultiService;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: 多选题错题
 * @Author: cc-admin
 * @Date:   2025-02-26
 * @Version: V1.0.0
 */
@Slf4j
@Api(tags="多选题错题")
@RestController
@RequestMapping("/choice/errorQuestionsMulti")
public class ChoiceErrorQuestionsMultiController extends BaseController<ChoiceErrorQuestionsMulti, IChoiceErrorQuestionsMultiService> {
	@Autowired
	private IChoiceErrorQuestionsMultiService choiceErrorQuestionsMultiService;

	@Autowired
	private IMultiChoiceQuestionsService multiChoiceQuestionsService;

	/**
	 * 分页列表查询
	 *
	 * @param key
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "多选题错题-分页列表查询")
	@ApiOperation(value="多选题错题-分页列表查询", notes="多选题错题-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(
								   @RequestParam(name="key",required = false) String key,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ChoiceErrorQuestionsMulti> queryWrapper = QueryGenerator.initQueryWrapper(new ChoiceErrorQuestionsMulti(), req.getParameterMap());
		if (StrUtil.isNotEmpty(key)) {
			queryWrapper.like("question",key);
		}
		Gson gson = new Gson();
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		java.lang.reflect.Type stringType = new TypeToken<List<String>>() {}.getType();
		java.lang.reflect.Type optionType = new TypeToken<List<option>>() {}.getType();
		java.lang.reflect.Type multiType = new TypeToken<List<List<String>>>() {}.getType();
		Map<String, String[]> parameterMap = req.getParameterMap();
		String[] catalogs = parameterMap.get("catalog");
		String tab = catalogs[0];
		queryWrapper.eq("subject_id",tab);
		queryWrapper.eq("create_by",user.getUsername());

		Page<ChoiceErrorQuestionsMulti> page = new Page<ChoiceErrorQuestionsMulti>(pageNo, pageSize);
		IPage<ChoiceErrorQuestionsMulti> pageList = choiceErrorQuestionsMultiService.page(page, queryWrapper);
		pageList.getRecords().forEach(multiChoice->{
			multiChoice.setOption(gson.fromJson(multiChoice.getOptionJson(),optionType));
			multiChoice.setAnswer(gson.fromJson(multiChoice.getAnswerJsonReal(),stringType));
			multiChoice.setUserAnswer(gson.fromJson(multiChoice.getAnswerJson(),stringType));
			// 定义每个选择题里面的下拉框
			ArrayList<Category> categories = new ArrayList<>();
			//当存在下拉数据时
			if (multiChoice.getCategoryIds() != null && multiChoice.getCategoryIdsText() != null) {
				String[] split = multiChoice.getCategoryIds().split(",");
				String[] split2 = multiChoice.getCategoryIdsText().split(",");
				for (int i = 0; i < split.length; i++) {
					Category category = new Category(split[i], split2[i]);
					categories.add(category);
				}
			}
			multiChoice.setCategorys(categories);
		});
		return Result.ok(pageList);
	}



	/**
	 * 分页列表查询
	 *
	 * @param key
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "没有讲解视频的多选题错题-分页列表查询")
	@ApiOperation(value="没有讲解视频的多选题错题-分页列表查询", notes="没有讲解视频的多选题错题-分页列表查询")
	@GetMapping(value = "/list2")
	public Result<?> getNoVideoChoice(
			@RequestParam(name="key",required = false) String key,
			@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
			@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
			HttpServletRequest req) {
		QueryWrapper<MultiChoiceQuestions> queryWrapper = QueryGenerator.initQueryWrapper(new MultiChoiceQuestions(), req.getParameterMap());
		if (StrUtil.isNotEmpty(key)) {
			queryWrapper.like("question",key);
		}

		Gson gson = new Gson();
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		java.lang.reflect.Type stringType = new TypeToken<List<String>>() {}.getType();
		java.lang.reflect.Type optionType = new TypeToken<List<option>>() {}.getType();
		java.lang.reflect.Type multiType = new TypeToken<List<List<String>>>() {}.getType();
		Map<String, String[]> parameterMap = req.getParameterMap();
		String[] catalogs = parameterMap.get("catalog");
		String tab = catalogs[0];
		queryWrapper.eq("subject_id",tab);
		List<String> noVideoIdList = choiceErrorQuestionsMultiService.getNoVideoList(tab);
		queryWrapper.in(noVideoIdList.size()>0,"id",noVideoIdList);
		Page<MultiChoiceQuestions> page = new Page<MultiChoiceQuestions>(pageNo, pageSize);
		IPage<MultiChoiceQuestions> pageList = multiChoiceQuestionsService.page(page, queryWrapper);
		pageList.getRecords().forEach(multiChoice->{
			multiChoice.setOption(gson.fromJson(multiChoice.getOptionJson(),optionType));
			multiChoice.setAnswer(gson.fromJson(multiChoice.getAnswerJson(),stringType));
			ArrayList<Category> categories = new ArrayList<>();
			//当存在下拉数据时
			if (multiChoice.getCategoryIds() != null && multiChoice.getCategoryIdsText() != null) {
				String[] split = multiChoice.getCategoryIds().split(",");
				String[] split2 = multiChoice.getCategoryIdsText().split(",");
				for (int i = 0; i < split.length; i++) {
					Category category = new Category(split[i], split2[i]);
					categories.add(category);
				}
			}
			multiChoice.setCategorys(categories);
		});
		return Result.ok(pageList);
	}

	/**
	 * 添加
	 *
	 * @param choiceErrorQuestionsMulti
	 * @return
	 */
	@AutoLog(value = "多选题错题-添加")
	@ApiOperation(value="多选题错题-添加", notes="多选题错题-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ChoiceErrorQuestionsMulti choiceErrorQuestionsMulti) {
        choiceErrorQuestionsMulti.setId(IdUtil.objectId());
        choiceErrorQuestionsMultiService.save(choiceErrorQuestionsMulti);
		return Result.ok("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param choiceErrorQuestionsMulti
	 * @return
	 */
	@AutoLog(value = "多选题错题-编辑")
	@ApiOperation(value="多选题错题-编辑", notes="多选题错题-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ChoiceErrorQuestionsMulti choiceErrorQuestionsMulti) {
        choiceErrorQuestionsMultiService.updateById(choiceErrorQuestionsMulti);
		return Result.ok("编辑成功!");
	}


	/**
	 *   复制
	 * @param id
	 * @param name
	 * @return
	 */
	@AutoLog(value = "多选题错题-复制")
	@ApiOperation(value="多选题错题-复制", notes="多选题错题-复制")
	@PostMapping(value = "/copy")
	public Result<?> copy(@RequestParam(name="id",required=true) String id,@RequestParam(name="name",required=true) String name) {
        ChoiceErrorQuestionsMulti choiceErrorQuestionsMulti = choiceErrorQuestionsMultiService.getById(id);
        choiceErrorQuestionsMulti.setId(IdUtil.fastUUID());
        choiceErrorQuestionsMulti.setCreateTime(new Date());
        choiceErrorQuestionsMultiService.save(choiceErrorQuestionsMulti);
		return Result.ok("复制成功！");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "多选题错题-通过id删除")
	@ApiOperation(value="多选题错题-通过id删除", notes="多选题错题-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        choiceErrorQuestionsMultiService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "多选题错题-批量删除")
	@ApiOperation(value="多选题错题-批量删除", notes="多选题错题-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.choiceErrorQuestionsMultiService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "多选题错题-通过id查询")
	@ApiOperation(value="多选题错题-通过id查询", notes="多选题错题-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        ChoiceErrorQuestionsMulti choiceErrorQuestionsMulti = choiceErrorQuestionsMultiService.getById(id);
		return Result.ok(choiceErrorQuestionsMulti);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param choiceErrorQuestionsMulti
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, ChoiceErrorQuestionsMulti choiceErrorQuestionsMulti) {
      return super.exportXls(request, choiceErrorQuestionsMulti, ChoiceErrorQuestionsMulti.class, "多选题错题");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, ChoiceErrorQuestionsMulti.class);
  }

}
